package com.lzh.runke.util;


import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFColor;

/**
 * @author LiuZiHao
 * @date 创建时间:2023年06月29日 10:54:09
 */
public class ExportUtil {

    /**
     * 设置XSSFCellStyle的样式
     * 默认无边框样式
     *
     * @param workbook
     * @param font        字体
     * @param xssfColor   背景颜色
     * @param hAlign      水平对齐方式
     * @param vAlign      垂直对齐方式
     * @param borderStyle 边框样式
     * @return
     */
    public static XSSFCellStyle createXCellStyle(Workbook workbook, Font font, XSSFColor xssfColor,
                                                 HorizontalAlignment hAlign, VerticalAlignment vAlign, BorderStyle borderStyle) {
        CellStyle cellStyle = workbook.createCellStyle();
        // 设置对齐样式
        hAlign = null == hAlign ? HorizontalAlignment.LEFT : hAlign;
        vAlign = null == vAlign ? VerticalAlignment.CENTER : vAlign;
        cellStyle.setAlignment(hAlign);
        cellStyle.setVerticalAlignment(vAlign);
        // 设置边框
        if (borderStyle != null) {
            cellStyle.setBorderTop(borderStyle);
            cellStyle.setBorderRight(borderStyle);
            cellStyle.setBorderBottom(borderStyle);
            cellStyle.setBorderLeft(borderStyle);
        }
        XSSFCellStyle xssfCellStyle = (XSSFCellStyle) cellStyle;
        if (font != null) {
            xssfCellStyle.setFont(font);
        }
        //设置背景色
        if (xssfColor != null) {
            xssfCellStyle.setFillForegroundColor(xssfColor);
            xssfCellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        }
        return xssfCellStyle;
    }
}
